home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / Src / Ch4 / CentText.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1999-04-10  |  11.1 KB  |  310 lines

  1. VERSION 5.00
  2. Begin VB.Form frmCentText 
  3.    Caption         =   "CentText"
  4.    ClientHeight    =   4200
  5.    ClientLeft      =   60
  6.    ClientTop       =   345
  7.    ClientWidth     =   6030
  8.    LinkTopic       =   "Form1"
  9.    ScaleHeight     =   4200
  10.    ScaleWidth      =   6030
  11.    StartUpPosition =   3  'Windows Default
  12.    Begin VB.CheckBox chkShowBoundingBox 
  13.       Caption         =   "Show Bounding Box"
  14.       Height          =   495
  15.       Left            =   240
  16.       TabIndex        =   6
  17.       Top             =   1080
  18.       Value           =   1  'Checked
  19.       Width           =   1695
  20.    End
  21.    Begin VB.CheckBox chkMarkCenters 
  22.       Caption         =   "Mark Centers"
  23.       Height          =   255
  24.       Left            =   240
  25.       TabIndex        =   5
  26.       Top             =   1680
  27.       Value           =   1  'Checked
  28.       Width           =   1695
  29.    End
  30.    Begin VB.PictureBox picText 
  31.       AutoRedraw      =   -1  'True
  32.       Height          =   3735
  33.       Left            =   2040
  34.       ScaleHeight     =   3675
  35.       ScaleWidth      =   3675
  36.       TabIndex        =   4
  37.       Top             =   240
  38.       Width           =   3735
  39.    End
  40.    Begin VB.TextBox txtText 
  41.       BeginProperty Font 
  42.          Name            =   "MS Sans Serif"
  43.          Size            =   8.25
  44.          Charset         =   0
  45.          Weight          =   400
  46.          Underline       =   0   'False
  47.          Italic          =   0   'False
  48.          Strikethrough   =   0   'False
  49.       EndProperty
  50.       Height          =   360
  51.       Left            =   840
  52.       TabIndex        =   3
  53.       Text            =   "Msg"
  54.       Top             =   600
  55.       Width           =   735
  56.    End
  57.    Begin VB.TextBox txtAngle 
  58.       BeginProperty Font 
  59.          Name            =   "MS Sans Serif"
  60.          Size            =   8.25
  61.          Charset         =   0
  62.          Weight          =   400
  63.          Underline       =   0   'False
  64.          Italic          =   0   'False
  65.          Strikethrough   =   0   'False
  66.       EndProperty
  67.       Height          =   330
  68.       Left            =   840
  69.       TabIndex        =   0
  70.       Text            =   "30"
  71.       Top             =   240
  72.       Width           =   735
  73.    End
  74.    Begin VB.Label Label1 
  75.       Caption         =   "Text"
  76.       Height          =   255
  77.       Index           =   1
  78.       Left            =   240
  79.       TabIndex        =   2
  80.       Top             =   600
  81.       Width           =   495
  82.    End
  83.    Begin VB.Label Label1 
  84.       Caption         =   "Angle"
  85.       Height          =   255
  86.       Index           =   0
  87.       Left            =   240
  88.       TabIndex        =   1
  89.       Top             =   240
  90.       Width           =   615
  91.    End
  92. Attribute VB_Name = "frmCentText"
  93. Attribute VB_GlobalNameSpace = False
  94. Attribute VB_Creatable = False
  95. Attribute VB_PredeclaredId = True
  96. Attribute VB_Exposed = False
  97. Option Explicit
  98. Private Type TEXTMETRIC
  99.     tmHeight As Long
  100.     tmAscent As Long
  101.     tmDescent As Long
  102.     tmInternalLeading As Long
  103.     tmExternalLeading As Long
  104.     tmAveCharWidth As Long
  105.     tmMaxCharWidth As Long
  106.     tmWeight As Long
  107.     tmOverhang As Long
  108.     tmDigitizedAspectX As Long
  109.     tmDigitizedAspectY As Long
  110.     tmFirstChar As Byte
  111.     tmLastChar As Byte
  112.     tmDefaultChar As Byte
  113.     tmBreakChar As Byte
  114.     tmItalic As Byte
  115.     tmUnderlined As Byte
  116.     tmStruckOut As Byte
  117.     tmPitchAndFamily As Byte
  118.     tmCharSet As Byte
  119. End Type
  120. Private Declare Function GetTextMetrics Lib "gdi32" Alias "GetTextMetricsA" (ByVal hdc As Long, lpMetrics As TEXTMETRIC) As Long
  121. ' Font weight constants.
  122. Private Const FW_DONTCARE = 0
  123. Private Const FW_THIN = 100
  124. Private Const FW_EXTRALIGHT = 200
  125. Private Const FW_LIGHT = 300
  126. Private Const FW_NORMAL = 400
  127. Private Const FW_MEDIUM = 500
  128. Private Const FW_SEMIBOLD = 600
  129. Private Const FW_BOLD = 700
  130. Private Const FW_EXTRABOLD = 800
  131. Private Const FW_HEAVY = 900
  132. Private Const FW_ULTRALIGHT = FW_EXTRALIGHT
  133. Private Const FW_REGULAR = FW_NORMAL
  134. Private Const FW_DEMIBOLD = FW_SEMIBOLD
  135. Private Const FW_ULTRABOLD = FW_EXTRABOLD
  136. Private Const FW_BLACK = FW_HEAVY
  137. ' Character set constants.
  138. Private Const ANSI_CHARSET = 0
  139. Private Const DEFAULT_CHARSET = 1
  140. Private Const SYMBOL_CHARSET = 2
  141. Private Const SHIFTJIS_CHARSET = 128
  142. Private Const OEM_CHARSET = 255
  143. ' Output precision constants.
  144. Private Const OUT_CHARACTER_PRECIS = 2
  145. Private Const OUT_DEFAULT_PRECIS = 0
  146. Private Const OUT_DEVICE_PRECIS = 5
  147. Private Const OUT_RASTER_PRECIS = 6
  148. Private Const OUT_STRING_PRECIS = 1
  149. Private Const OUT_STROKE_PRECIS = 3
  150. Private Const OUT_TT_ONLY_PRECIS = 7
  151. Private Const OUT_TT_PRECIS = 4
  152. ' Clipping precision constants.
  153. Private Const CLIP_CHARACTER_PRECIS = 1
  154. Private Const CLIP_DEFAULT_PRECIS = 0
  155. Private Const CLIP_EMBEDDED = &H80
  156. Private Const CLIP_LH_ANGLES = &H10
  157. Private Const CLIP_STROKE_PRECIS = 2
  158. Private Const CLIP_TO_PATH = 4097
  159. Private Const CLIP_TT_ALWAYS = &H20
  160. ' Character quality constants.
  161. Private Const DEFAULT_QUALITY = 0
  162. Private Const DRAFT_QUALITY = 1
  163. Private Const PROOF_QUALITY = 2
  164. ' Pitch and family constants.
  165. Private Const DEFAULT_PITCH = 0
  166. Private Const FIXED_PITCH = 1
  167. Private Const VARIABLE_PITCH = 2
  168. Private Const TRUETYPE_FONTTYPE = &H4
  169. Private Const FF_DECORATIVE = 80  '  Old English, etc.
  170. Private Const FF_DONTCARE = 0     '  Don't care or don't know.
  171. Private Const FF_MODERN = 48      '  Constant stroke width, serifed or sans-serifed.
  172. Private Const FF_ROMAN = 16       '  Variable stroke width, serifed.
  173. Private Const FF_SCRIPT = 64      '  Cursive, etc.
  174. Private Const FF_SWISS = 32       '  Variable stroke width, sans-serifed.
  175. Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
  176. Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
  177. Private Declare Function CreateFont Lib "gdi32" Alias "CreateFontA" (ByVal H As Long, ByVal W As Long, ByVal E As Long, ByVal O As Long, ByVal W2 As Long, ByVal I As Long, ByVal u As Long, ByVal S As Long, ByVal C As Long, ByVal OP As Long, ByVal CP As Long, ByVal Q As Long, ByVal PAF As Long, ByVal F As String) As Long
  178. ' Draw a rotated string centered at the indicated
  179. ' position using the indicated font parameters.
  180. Private Sub CenterText(ByVal pic As PictureBox, ByVal xmid As Single, ByVal ymid As Single, ByVal txt As String, ByVal nHeight As Long, ByVal nWidth As Long, ByVal nEscapement As Long, ByVal fnWeight As Long, ByVal fbItalic As Long, ByVal fbUnderline As Long, ByVal fbStrikeOut As Long, ByVal fbCharSet As Long, ByVal fbOutputPrecision As Long, ByVal fbClipPrecision As Long, ByVal fbQuality As Long, ByVal fbPitchAndFamily As Long, ByVal lpszFace As String)
  181. Const PI = 3.14159265
  182. Dim newfont As Long
  183. Dim oldfont As Long
  184. Dim text_metrics As TEXTMETRIC
  185. Dim internal_leading As Single
  186. Dim total_hgt As Single
  187. Dim text_wid As Single
  188. Dim text_hgt As Single
  189. Dim text_bound_wid As Single
  190. Dim text_bound_hgt As Single
  191. Dim total_bound_wid As Single
  192. Dim total_bound_hgt As Single
  193. Dim theta As Single
  194. Dim phi As Single
  195. Dim x1 As Single
  196. Dim y1 As Single
  197. Dim x2 As Single
  198. Dim y2 As Single
  199. Dim x3 As Single
  200. Dim y3 As Single
  201. Dim x4 As Single
  202. Dim y4 As Single
  203.     ' Create the font.
  204.     newfont = CreateFont(nHeight, nWidth, nEscapement, 0, fnWeight, fbItalic, fbUnderline, fbStrikeOut, fbCharSet, fbOutputPrecision, fbClipPrecision, fbQuality, fbPitchAndFamily, lpszFace)
  205.     oldfont = SelectObject(pic.hdc, newfont)
  206.     ' Get the font metrics.
  207.     GetTextMetrics pic.hdc, text_metrics
  208.     internal_leading = pic.ScaleY(text_metrics.tmInternalLeading, vbPixels, pic.ScaleMode)
  209.     total_hgt = pic.ScaleY(text_metrics.tmHeight, vbPixels, pic.ScaleMode)
  210.     text_hgt = total_hgt - internal_leading
  211.     text_wid = pic.TextWidth(txt)
  212.     ' Get the bounding box geometry.
  213.     theta = nEscapement / 10 / 180 * PI
  214.     phi = PI / 2 - theta
  215.     text_bound_wid = text_hgt * Cos(phi) + text_wid * Cos(theta)
  216.     text_bound_hgt = text_hgt * Sin(phi) + text_wid * Sin(theta)
  217.     total_bound_wid = total_hgt * Cos(phi) + text_wid * Cos(theta)
  218.     total_bound_hgt = total_hgt * Sin(phi) + text_wid * Sin(theta)
  219.     ' Find the desired center point.
  220.     x1 = xmid
  221.     y1 = ymid
  222.     ' Subtract half the height and width of the text
  223.     ' bounding box. This puts (x1, y2) in the upper
  224.     ' left corner of the text bounding box.
  225.     x1 = x1 - text_bound_wid / 2
  226.     y1 = y1 - text_bound_hgt / 2
  227.     ' The start position's X coordinate belongs at
  228.     ' the left edge of the text bounding box, so
  229.     ' x1 is correct. Move the Y coordinate down to
  230.     ' its start position.
  231.     y1 = y1 + text_wid * Sin(theta)
  232.     ' Find the other points on the text bounding box.
  233.     x2 = x1 + text_wid * Cos(theta)
  234.     y2 = y1 - text_wid * Sin(theta)
  235.     x3 = x2 + text_hgt * Cos(phi)
  236.     y3 = y2 + text_hgt * Sin(phi)
  237.     x4 = x3 + -text_wid * Cos(theta)
  238.     y4 = y3 + text_wid * Sin(theta)
  239.     ' See if we should draw the bounding box.
  240.     If chkShowBoundingBox.Value = vbChecked Then
  241.         ' Draw the text bounding box.
  242.         pic.Line (x1, y1)-(x2, y2)
  243.         pic.Line -(x3, y3)
  244.         pic.Line -(x4, y4)
  245.         pic.Line -(x1, y1)
  246.     End If
  247.     ' See if we should mark the text and PictureBox
  248.     ' center positions.
  249.     If chkMarkCenters.Value = vbChecked Then
  250.         ' Draw lines to mark the center of the PictureBox.
  251.         pic.Line (0, 0)-(pic.ScaleWidth, pic.ScaleHeight)
  252.         pic.Line (0, pic.ScaleHeight)-(pic.ScaleWidth, 0)
  253.         ' Draw lines to mark the center of the text rectangle.
  254.         pic.Line (x1, y1)-(x3, y3)
  255.         pic.Line (x2, y2)-(x4, y4)
  256.     End If
  257.     ' Move (x1, y1) to the start corner of the
  258.     ' outer bounding box.
  259.     x1 = x1 - (total_bound_wid - text_bound_wid)
  260.     y1 = y1 - (total_bound_hgt - text_bound_hgt)
  261.     ' Display the text.
  262.     pic.CurrentX = x1
  263.     pic.CurrentY = y1
  264.     pic.Print txt
  265.     ' Reselect the old font and delete the new one.
  266.     newfont = SelectObject(pic.hdc, oldfont)
  267.     If DeleteObject(newfont) = 0 Then
  268.         Beep
  269.         MsgBox "Error deleting font object.", vbExclamation
  270.     End If
  271. End Sub
  272. ' Draw the rotated text centered in the PictureBox.
  273. Private Sub DrawText()
  274. Dim escapement As Long
  275.     ' Clear the display.
  276.     picText.Line (0, 0)-(picText.ScaleWidth, picText.ScaleHeight), vbWhite, BF
  277.     ' Get the text and angle.
  278.     ' Watch for non-numeric values.
  279.     On Error Resume Next
  280.     escapement = 10 * CInt(txtAngle.Text)
  281.     On Error GoTo 0
  282.     CenterText picText, _
  283.         picText.ScaleWidth / 2, picText.ScaleHeight / 2, _
  284.         txtText.Text, 120, 0, escapement, _
  285.         FW_NORMAL, False, False, False, _
  286.         DEFAULT_CHARSET, OUT_TT_ONLY_PRECIS, _
  287.         CLIP_DEFAULT_PRECIS, PROOF_QUALITY, _
  288.         TRUETYPE_FONTTYPE, "Times New Roman"
  289. End Sub
  290. ' Draw the text.
  291. Private Sub chkMarkCenters_Click()
  292.     DrawText
  293. End Sub
  294. ' Draw the text.
  295. Private Sub chkShowBoundingBox_Click()
  296.     DrawText
  297. End Sub
  298. ' Display the text.
  299. Private Sub Form_Load()
  300.     DrawText
  301. End Sub
  302. ' Display the text at the new angle.
  303. Private Sub txtAngle_Change()
  304.     DrawText
  305. End Sub
  306. ' Display the new text.
  307. Private Sub txtText_Change()
  308.     DrawText
  309. End Sub
  310.